﻿using System;
using System.Globalization;

namespace EIM_Accounting.Common.StringUtility
{
    public class StringHelper
    {
        public static string getDecimalStr(object obj)
        {
            string backStr = string.Empty;
            try
            {
                backStr = decimal.Parse(obj.ToString()).ToString("#,###.##");
            }
            catch
            {
                backStr = string.Empty;
            }
            return backStr;
        }

        public static string getDateStrWithoutNewLine(object start, object end)
        {
            string backStr = string.Empty;

            try
            {
                string startDay = string.Empty;
                if (start != null)
                {
                    try
                    {
                        startDay = DateTime.ParseExact(start.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy年MM月dd日");
                    }
                    catch { }
                }

                string endDay = string.Empty;
                if (end != null)
                {
                    try
                    {
                        endDay = DateTime.ParseExact(end.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy年MM月dd日");
                    }
                    catch { }
                }

                if (endDay != string.Empty && startDay != string.Empty)
                {
                    backStr = startDay + "~" + endDay;
                }
                else
                {
                    backStr = startDay + endDay;
                }
            }
            catch
            {
                backStr = string.Empty;
            }

            return backStr;
        }

        public static string getDateStrWithNewLine(object start, object end)
        {
            string backStr = string.Empty;

            try
            {
                string startDay = string.Empty;
                if (start != null)
                {
                    try
                    {
                        startDay = DateTime.ParseExact(start.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy年MM月dd日");
                    }
                    catch { }
                }

                string endDay = string.Empty;
                if (end != null)
                {
                    try
                    {
                        endDay = DateTime.ParseExact(end.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy年MM月dd日");
                    }
                    catch { }
                }

                if (endDay != string.Empty && startDay != string.Empty)
                {
                    backStr = startDay + Environment.NewLine + "~" + Environment.NewLine + endDay;
                }
                else
                {
                    backStr = startDay + endDay;
                }
            }
            catch
            {
                backStr = string.Empty;
            }

            return backStr;
        }
    }
}